/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.springtree.spectrum.visa.persistence.dao;

import co.springtree.spectrum.visa.persistence.entities.CommandRuleDTO;
import co.springtree.spectrum.visa.persistence.entities.RuleDTO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Ricardo
 */
public class RuleDAO extends AbstractDAO<RuleDTO>{

    private static Logger logger = Logger.getLogger("RuleDAO");
    @Override
    public void insert(RuleDTO object) throws SQLException {
        try {
            connection = getConnectionPool().getConnection();
            sQLQuery = "INSERT INTO rule (ruleName) VALUES (?)";
            statement = connection.prepareStatement(sQLQuery);
            statement.setString(1, object.getRuleName());
            statement.executeUpdate();
        } catch (SQLException e) {
            logger.error(e);
        } finally {
            getConnectionPool().freeConnection(connection);
        }
    }

    @Override
    public void update(RuleDTO object) throws SQLException {
        try {
            connection = getConnectionPool().getConnection();
            sQLQuery = "UPDATE rule SET rulename = ?, rulename=?";
            statement = connection.prepareStatement(sQLQuery);
            statement.setString(1, object.getRuleName());
            statement.executeUpdate();
        } catch (SQLException e) {
           logger.error(e);
        } finally {
            getConnectionPool().freeConnection(connection);
        }
    }

    @Override
    public void delete(RuleDTO object) throws SQLException {
        try {
            connection = getConnectionPool().getConnection();
            sQLQuery = "DELETE FROM rule WHERE rulename = ?";
            statement = connection.prepareStatement(sQLQuery);
            statement.setString(1, object.getRuleName());
            statement.executeUpdate();
        } catch (SQLException e) {
            logger.error(e);
        } finally {
            getConnectionPool().freeConnection(connection);
        }
    }

    @Override
    public RuleDTO select(RuleDTO ruleSearch) throws SQLException {
        RuleDTO rule = null;
        try {
            connection = getConnectionPool().getConnection();
            sQLQuery = "SELECT * FROM rule WHERE ruleName = ?";
            statement = connection.prepareStatement(sQLQuery);
            statement.setString(1, ((RuleDTO) ruleSearch).getRuleName());
            rs = statement.executeQuery();
            if (rs.next()) {
                rule = new RuleDTO();
                rule.setRuleName(rs.getString("rulename"));
            }
        } catch (SQLException e) {
            logger.error(e);
        } finally {
            getConnectionPool().freeConnection(connection);
        }

        return rule;
    }

    @Override
    public Collection<RuleDTO> selectAll() throws SQLException {
        Collection<RuleDTO> ruleColl = new ArrayList<RuleDTO>();
        CommandRuleDAO commandRuleDAO = new CommandRuleDAO();
        try {
            connection = getConnectionPool().getConnection();
            sQLQuery = "SELECT * FROM rule";
            statement = connection.prepareStatement(sQLQuery);
            rs = statement.executeQuery();
            while (rs.next()) {
                RuleDTO rule = new RuleDTO();
                rule.setRuleName(rs.getString("ruleName"));
                Collection<CommandRuleDTO> commandRuleColl = commandRuleDAO.selectAll(rule);
                ruleColl.add(rule);
            }
        } catch (SQLException e) {
            logger.error(e);
        } finally {
            getConnectionPool().freeConnection(connection);
        }

        return ruleColl;
    }

    private void getRuleCommands(){
        try {
            CommandRuleDAO commandRuleDAO = new CommandRuleDAO();
            Collection<CommandRuleDTO> commandColl = commandRuleDAO.selectAll();
        } catch (SQLException ex) {
            logger.error(ex);
        }
    }
    @Override
    public void createTable(boolean dropTable) throws Exception {
        try {
            connection = getConnectionPool().getConnection();
            sQLQuery = null;
            if (dropTable == true) {
                sQLQuery = "DROP TABLE IF EXISTS rule";
                statement = connection.prepareStatement(sQLQuery);
                statement.executeUpdate();
            }
            sQLQuery = "CREATE TABLE rule ("
                    + "  rulename varchar(255) NOT NULL,"
                    + "  PRIMARY KEY (rulename))";

            statement = connection.prepareStatement(sQLQuery);
            statement.executeUpdate();
        } catch (SQLException e) {
            logger.error(e);
        } finally {
            getConnectionPool().freeConnection(connection);
        }
    }
    
    
    
}
